package com.agv.core.dao.lectorArchivos;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import org.springframework.jdbc.core.JdbcTemplate;

import com.agv.core.exception.AgvException;
import com.agv.core.lectorDeGlobalizadoresImpl.FormatoDeFechas;

public class TransaccionUnoDAOImpl implements ITransaccionUnoDAO {

	JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void insertarRegistroDesdePNR(HashMap<String, String> data)
	throws AgvException {
		StringBuilder builder = new StringBuilder();
		String query;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

		
		
		
		builder.append("INSERT INTO cxc.t001_cxc_trn_transac(");
		builder.append("	    fh_reserva, id_trn_cxc, moneda_docto )");
		builder.append("    VALUES (?, nextval('cxc.t001_cxc_trn_transac_seq'), ?);");

		query = builder.toString();
		

		
		
//		try {
//			jdbcTemplate.update(query, new Object[] {format.parse(data.get("FECHA_RESERVA")), 
//					data.get("MONEDA")});
//		} catch (Exception e) {
//			System.out.println(e.getMessage());		
//		}


	}

	@Override
	public void insertarRegistroDesdeDAT(HashMap<String, String> data)
	throws AgvException {
		int moneda;
		String query;
		StringBuilder builder = new StringBuilder();
		FormatoDeFechas formatoDeFechas = new FormatoDeFechas();
		
//		Date FECHA_RESERVA = formatoDeFechas.agregarAnio(data.get("FECHA_RESERVA"), 
//				data.get("FECHA_EMISION"));
//		Date FECHA_EMISION = formatoDeFechas.obtenerFecha(data.get("FECHA_EMISION"));
//		
		builder.append("INSERT INTO cxc.t001_cxc_trn_transac(");
		builder.append("	    id_trn_cxc , fh_reserva, fh_emision, moneda_docto )");
		builder.append("    VALUES ( nextval('cxc.t001_cxc_trn_transac_seq'),?, ?,?);");

		query = builder.toString();
		
		System.out.println("reserva "+data.get("FECHA_RESERVA"));
		System.out.println("emision "+data.get("FECHA_EMISION"));
		System.out.println("moneda "+data.get("MONEDA"));
		
		moneda = jdbcTemplate.queryForInt("SELECT id_divisa FROM ctb.t001_ctb_cfg_moneda where cd_divisa = ?;",
				new Object[]{data.get("MONEDA")});
		
//		try {
//			jdbcTemplate.update(query, new Object[] {FECHA_RESERVA, 
//					FECHA_EMISION, moneda});
//		} catch (Exception e) {
//			System.out.println(e.getMessage());		
//		}		
	}

	@Override
	public void insertarRegistroDesdeTXT(HashMap<String, String> data)
	throws AgvException {
		StringBuilder builder = new StringBuilder();
		String query;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

		builder.append("INSERT INTO cxc.t001_cxc_trn_transac(");
		builder.append("	    fh_reserva, id_trn_cxc, moneda_docto )");
		builder.append("    VALUES (?, nextval('cxc.t001_cxc_trn_transac_seq'), ?);");

		query = builder.toString();
		try {
			jdbcTemplate.update(query, new Object[] {format.parse(data.get("FECHA_RESERVA")), 
					data.get("MONEDA")});		
		} catch (Exception e) {
			System.out.println(e.getMessage());		
		}		
	}


}
